#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
    bool backspaceCompare(string s, string t) {
        return buildString(s) == buildString(t);
    }

private:
    string buildString(const string& str) {
        string stack;
        for (char ch : str) {
            if (ch != '#') {
                stack.push_back(ch);
            }
            else if (!stack.empty()) {
                stack.pop_back();
            }
        }
        return stack;
    }
};